package com.jet.chat.command;

import java.util.regex.Matcher;

import com.jet.chat.dbquery.ChatQueryGetWhisperHistory;
import com.jet.chat.dbquery.ChatQueryGetWhisperHistoryParams;
import com.jet.chat.dbquery.ChatQueryGetWhisperHistoryResult;
import com.jet.chat.dbquery.ChatQueryGetWhisperHistoryResultRaw;
import com.jet.chat.message.Message;
import com.jet.chat.server.ChatServer;
import com.jet.chat.server.ChatSession;


public class ChatCommandViewWhisperHistory extends ChatCommandAbstract {
	public ChatCommandViewWhisperHistory() {
		super("^/\\w+\\s([\\w\\.-]+)$");
	}

	@Override
	protected void executeCommand(Matcher matcher, ChatServer chatServer, ChatSession chatSession, Message chatMessage) throws Exception {
		String responce = "";
		String timestamp = matcher.group(1);
		ChatQueryGetWhisperHistoryResult result = (ChatQueryGetWhisperHistoryResult) new ChatQueryGetWhisperHistory().execute(chatServer.getDBConnectionService(), new ChatQueryGetWhisperHistoryParams(chatSession.getUserId(), timestamp));
		for (ChatQueryGetWhisperHistoryResultRaw raw : result.getResult()) {
			responce = responce + raw.getTimestamp() + "\t" + raw.getUsername() + " whispers: " + raw.getContent() + "\r\n";
		}
		chatSession.getQueue().addMessage(responce);
	}

}
